@extends('layouts.header')
@section('style')
    .layui-layer-btn .layui-layer-btn1 {
    border-color: #dd514c;
    background-color: #dd514c;
    color: #fff;
    }
@endsection
@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 检测业务 <span class="c-gray en">&gt;</span> <a href="javascript:;" onclick="location.href='/{{config('constant.name_admin')}}/contract/indexCheck';">审核报告</a><span class="c-gray en">&gt;</span> 点击进入【{{ $contractInfo->client_name }}】
        <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);"
           title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
        <a class="btn btn-primary radius r mr-10" style="line-height:1.6em;margin-top:3px" href="javascript:self.location=document.referrer;">
            <i class="Hui-iconfont Hui-iconfont-arrow1-left"></i>&nbsp;&nbsp;返回
        </a>
    </nav>
    <div class="page-container">
        <div class="cl pd-5 bg-1 bk-gray mt-10">
            <span class="l">
                <a class="btn btn-primary radius" href="javascript:;" onclick="check_batch()">批量审核</a>
            </span>
            <span class="l ml-20">
            <a class="btn btn-primary radius" href="javascript:;" onclick="download_report({{ $contractInfo->id }})">查看检测报告</a>
            </span>
        </div>
        <div class="mt-10">
            <table class="table table-border table-bordered table-bg table-hover table-responsive" id="datatable">
                <thead>
                {{--
                <tr>
                    <th scope="col" colspan="5">客户管理</th>
                </tr> --}}
                <tr class="text-c">
                    <th>
                        <input type="checkbox" id="checkAll">
                    </th>
                    <th>条形码</th>
                    <th>样品名称</th>
                    <th>额定电压</th>
                    <th>样品状态</th>
                    <th>结论</th>
                    <th>提交时间</th>
                    {{-- <th>主检人</th> --}}
                    <th>状态</th>
                    <th>审核时间</th>
                    <th>审核次数</th>
                    <th>审核人</th>
                    <th>试验日期</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div>
        {{-- <div class="cl pd-5 bg-1 bk-gray mt-15">
                <input type="button" value="确定" id="btn_ok" class="btn btn-success radius">
        </div> --}}
    </div>

    <script type="text/javascript">
        var table, appname = '{{config('constant.app_name')}}', status = '{{$_GET['status']}}';
        $(function () {

            $.fn.dataTable.ext.errMode = 'throw';//datatable抛异常的方式，在console中输出，默认以alert的方式弹出
            table = $('#datatable').DataTable({
                order: [[1, 'asc']],//默认第几个排序
                // ordering: false, // 是否排序
                "pageLength": 10,
                // "dom": '<"toolbar f-l pb-15">frtip',
                // "lengthChange": false,
                // "stateSave": true,// 状态保存 - 再次加载页面时还原表格状态
                // "stateDuration": -1,//仅保留当前会话的数据
                // "paging":false, //禁用分页
                serverSide: false,//服务器端处理模式——此模式下如：过滤、分页、排序的处理都放在服务器端进行。
                // responsive: true,//响应式，需要插件
                // fixedHeader: true,//表头固定，需要插件
                // autoWidth: false,
                ajax: "{{request()->fullUrl()}}",
                initComplete: function (settings, json) {
                    //当表格完成加载绘制完成后执行此方法
                    //仅初次表格数据加载完成后执行
                    // $(row).addClass( 'text-c' );
                    $('#datatable_filter label input').attr('placeholder', '批次/样品名称')
                },
                processing: true,//加载数据时候是否显示进度条
                language: {url: '/admin/lib/datatables/dataTables_Chinese_Lang.json'},
                createdRow: function (row, data, dataIndex) {
                    //当tr元素被创建（所有的td元素已经插入进去），或者给tr绑定事件，该回调函数被执行
                    //$(row).addClass( 'text-l' );
                },

                columns: [
                    { //复选框
                        data: null, class: "text-c",
                        render: function (data, type, row, meta) {
                            return '<input type="checkbox" value="' + row.id + '">';
                        }, searchable: false, orderable: false
                    },
                    {data: "bar_code", class: "text-c"},
                    {data: "tool_name", class: "text-c"},
                    {data: "voltage", class: "text-c"},
                    {data: "sample_status", class: "text-c"},
                    {
                        data: "res", class: "text-c",
                        render: function (data, type, row, meta) {
                            if (data == 0) {
                                return '';
                            } else if (data == 1) {
                                return '<span class="badge badge-primary radius">合格</span>';
                            } else if (data == 2) {
                                return '<span class="badge badge-danger radius">不合格</span>';
                            }
                        }
                    },
                    {data: "submit_time", class: "text-c"},
                    {
                        data: "status0", class: "text-c",
                        render: function (data, type, row, meta) {
                            if (data == -1) {
                                return '<span class="badge badge-danger radius">已驳回</span>';
                            } else if (data == 0) {
                                return '<span class="badge badge-primary radius">未提交</span>';
                            } else if (data == 1) {
                                return '<span class="badge badge-primary radius">待审核</span>';
                            } else if (data == 2) {
                                return '<span class="badge badge-default radius">审核通过</span>';
                            } else if (data == 3) {
                                return '<span class="badge badge-default radius">未试验</span>';
                            } else if (data == 4) {
                                return '<span class="badge badge-default radius">试验中</span>';
                            } else {
                                return '<span class="badge badge-default radius">未知状态</span>';
                            }
                        }
                    },
                    {data: "check_time", class: "text-c", defaultContent: '/'},
                    {
                        data: "check_times", class: "text-c",
                        render: function (data, type, row, meta) {
                            if (data == 0) {
                                return '<span class="badge badge-default radius">' + data + '</span>';
                            }
                            if (data == 1) {
                                return '<span class="badge badge-primary radius">' + data + '</span>';
                            } else {
                                return '<span class="badge badge-danger radius">' + data + '</span>';
                            }
                        }
                    },
                    {data: "audit_man", class: "text-c"},
                    {data: "test_time", class: "text-c"},
                    {
                        data: null, class: "text-l",
                        render: function (data, type, row, meta) {
                            let str = '<a class="ml-5 btn btn-primary radius size-MINI" href="javascript:;" onclick="testData_edit(\'结果查看\',\'/{{config('constant.name_admin')}}/itemData/view\',\'' + row.id + '\')">结果查看</a>';
                            //如果已经批准通过，只能查看结果
                            if (status == 14) {
                                return str;
                            }
                            var classgo = '', classbak = '';
                            if (row.status0 == 2) {//审核通过
                                classgo = ' disabled ';
                                classbak = ' btn-danger ';
                            } else if (row.status0 == -1) {//审核驳回
                                classgo = ' btn-success ';
                                classbak = ' disabled ';
                            } else {
                                classgo = ' btn-success ';
                                classbak = ' btn-danger ';
                            }
                            str += ' <a class="ml-5 btn radius size-MINI ' + classgo + '" href="javascript:;" onclick="check_one(' + row.id + ',2)">通过</a>' +
                                '<a class="ml-5 btn radius size-MINI ' + classbak + '" href="javascript:;" onclick="check_one(' + row.id + ',-1)">驳回</a>';
                            if (row.status0 === -1 || row.back_count > 0) {
                                str += '<a class="ml-5 btn radius size-MINI btn-danger" href="javascript:;" onclick="show_baklog(' + row.id + ')">驳回记录</a>';
                            }
                            if (row.third_status === 1 || row.third_status===2) {
                                str += '<a class="ml-5 btn radius size-MINI btn-success" href="javascript:;" onclick="up_data(' + row.id + ')">上传结果</a>';
                            }
                            return str;
                        }, searchable: false
                    },
                ],
                columnDefs: [{
                    "targets": "_all",
                    "defaultContent": "/"
                }]
            });
            // <th>条形码</th>
            // <th>样品名称</th>
            // <th>电压等级</th>
            // <th>样品状态</th>
            // <th>结论</th>
            // <th>操作</th>
            /*  draw  在表格完成绘制后触发*/
            table.on('draw', function () {
                /*         var info = table.page.info();
                        //如果最后一页仅剩一条记录，而且不是在第一页
                        if((info.start == info.end) && info.end > 1){
                            //跳转到上一页
                            $('#datatable_previous').click();
                        } */
                $('.skin-minimal input').iCheck({
                    checkboxClass: 'icheckbox-blue',
                    radioClass: 'iradio-blue',
                    increaseArea: '20%'
                });

                //清除table中 CheckBox的对勾
                $("#datatable").find("input[type=checkbox]").iCheck('uncheck');

                iCheck_checkAll();//iCheck 全选
            });


        });

        function up_data(id){
            $.ajax({
                type: 'get',
                url: '/{{config('constant.name_admin')}}/RequestGwData/upData',
                // dataType: 'json',
                data: {
                    id: id,
                    _token: '{{ csrf_token() }}',
                },
                success: function (data) {
                    console.log(data);
                    if (data.code === '1') {
                        layer.msg(data.msg, {icon: 2, time: 2000});
                    } else {
                        layer.msg(data.msg, {icon: 1, time: 2000});
                    }
                },
                error: function (data) {
                    layer.msg('操作失败!', {icon: 2, time: 2000});
                },
                beforeSend: function () {
                    //0.4透明度 白色遮罩
                    index2 = layer.load(2, {shade: [0.05, '#000']});
                },
                complete: function () {
                    layer.close(index2);
                },
            });
        }

        /*下载报告*/
        function download_report(id, pdf = 0, newer = 0) {
            if (newer == 1) {
                layer.confirm('请谨慎操作！重新生成报告，旧报告将会被覆盖！', {title: '请谨慎操作', icon: 3}, function (index) {
                    window.open('/{{config('constant.name_admin')}}/exportReport/export?id=' + id + '&toPdf=' + pdf + '&newer='+newer, '_blank');
                });
            }else{
                window.open('/{{config('constant.name_admin')}}/exportReport/export?id=' + id + '&toPdf=' + pdf, '_blank');
            }
        }

        /*试验数据  修改*/
        function testData_edit(title, url, id, w, h) {
            layer_show(title, url + '?id=' + id, w, h);
        }

        function batch_check() {
            var doms = $("tbody input:checkbox:checked");
            var l = doms.length;

            layer.msg(l + '', {time: 2000});
        }

        /*表格重新加载*/
        function table_reload(bool) {
            if (bool) {
                //true时，跳到第一页
                table.ajax.reload();
            } else {
                //false时，停留在原来页
                table.ajax.reload(null, false);
            }
        }

        /*单个审核*/
        function check_one(id, status) {
            var index2;
            if (status === -1) {
                //prompt层
                layer.prompt({title: '输入驳回原因，并确认', formType: 2}, function (text, index) {
                    check_one_ajax(id, status, text, index);
                });
            } else {
                check_one_ajax(id, status);
            }

            function check_one_ajax(id, status, reason = '', index = '') {
                $.ajax({
                    type: 'post',
                    url: '/{{config('constant.name_admin')}}/sampleInfo/oneCheck',
                    // dataType: 'json',
                    data: {
                        id: id,
                        status: status,
                        reason: reason,     //驳回原因
                        batch_id: '{{ $_GET['id'] }}',
                        _token: '{{ csrf_token() }}',
                        _form_token: ' {{csrf_form_token()}} '
                    },
                    success: function (data) {
                        // console.log(data);
                        if (data === '1') {
                            table_reload(0);//0停留在当前页，1回到第一页
                            layer.close(index);
                            layer.msg('审核完成!', {icon: 1, time: 1000});
                        } else {
                            layer.msg('操作失败!', {icon: 2, time: 2000});
                        }
                        res_func()
                    },
                    error: function (data) {
                        ajax_error(data);
                        res_func()
                    },
                    beforeSend: function () {
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2, {shade: [0.05, '#000']});
                    },
                    complete: function () {
                        layer.close(index2);
                    },
                });
            }
        }

        /*批量审核 询问框*/
        function check_batch() {
            var doms = $("tbody tr > td:first-child input:checkbox:checked");
            var l = doms.length;
            if (l == 0) {
                layer.msg('请先勾选一条记录', {time: 2000});
                return;
            }

            var checkID = [];//定义一个空数组
            doms.each(function (i) {//把所有被选中的复选框的值存入数组
                checkID[i] = $(this).val();
            });

            layer.confirm('您勾选了 ' + l + ' 条记录', {
                btn: ['批量通过', '批量驳回'], //按钮
                btnAlign: 'c',//按钮居中对齐
                title: '批量审核',
            }, function () {
                check_batch_real(2, checkID);//通过
            }, function () {
                check_batch_real(-1, checkID);//驳回
            });
        }

        /*批量审核*/
        function check_batch_real(status, checkID) {
            var index2;

            $.ajax({
                type: 'post',
                url: '/{{config('constant.name_admin')}}/sampleInfo/batchCheck',
                // dataType: 'json',
                data: {
                    status: status,
                    checkID: checkID,
                    batch_id: '{{ $_GET['id'] }}',
                    _token: '{{ csrf_token() }}',
                    _form_token: ' {{csrf_form_token()}} '
                },
                success: function (data) {
                    // console.log(data);
                    if (data === '1') {
                        table_reload(0);//0停留在当前页，1回到第一页
                        layer.msg('审核完成!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('操作失败!', {icon: 2, time: 2000});
                    }
                    res_func()
                },
                error: function (data) {
                    ajax_error(data);
                    res_func()
                },
                beforeSend: function () {
                    //0.4透明度 白色遮罩
                    index2 = layer.load(2, {shade: [0.05, '#000']});
                },
                complete: function () {
                    layer.close(index2);
                },
            });
        }
        // 刷新页面获取token
        function res_func(){
            setTimeout(function (){
                window.location.reload()
            },2000)
        }
        /*删除*/
        function del(id) {
            var index2;

            layer.confirm('确认要删除吗？', function (index) {
                $.ajax({
                    type: 'GET',
                    url: '/{{config('constant.name_admin')}}/client/del' + '?id=' + id,
                    // dataType: 'json',
                    success: function (data) {
                        // console.log(data);
                        if (data === '1') {
                            table_reload(0);//0停留在当前页，1回到第一页
                            layer.msg('删除成功!', {icon: 1, time: 2000});
                        } else {
                            layer.msg('删除失败!', {icon: 2, time: 2000});
                        }
                    },
                    error: function (data) {
                        ajax_error(data);
                    },
                    beforeSend: function () {
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2, {shade: [0.05, '#000']});
                    },
                    complete: function () {
                        layer.close(index2);
                    },
                });
            });
        }

        /*编辑*/
        function edit(title, url, id, w, h) {
            layer_show(title, url + '?id=' + id, w, h);
        }

        /*添加*/
        function add(title, url, w, h) {
            layer_show(title, url, w, h);
        }

        /*查看驳回原因*/
        function show_baklog(id) {
            let url = '/{{config('constant.name_admin')}}/sampleinfo/rejectLog/0/' + id;
            layer_show('驳回原因', url, 500, 400);
        }

    </script>
@endsection
